import { logger } from "./logger"

export abstract class CoreError<T> extends Error {
    constructor(public code: number, public message: string, public data: T | undefined = undefined) {
        super(message)
    }
    toJson() {
        return {
            success: false,
            code: this.code,
            message: this.message,
            data: this.data
        }
    }
}

export class ForbiddenError extends CoreError<object | string> {
    constructor(data: object | string | undefined = undefined) {
        super(403, 'FORBIDDEN', data)
        logger.error(`403 FORBIDDEN ${JSON.stringify(data, null, 2)}`)
    }
}

export class NotFoundError extends CoreError<string> {
    constructor(filePath: string) {
        super(404, 'NOT FOUND', filePath)
        logger.error(`404 Not Found ${filePath}`)
    }
}

export class JsonParseError extends CoreError<string> {
    constructor(msg: string) {
        super(500, 'JsonParseError', msg)
    }
}

export class DataParseError extends CoreError<object> {
    constructor(msg: object) {
        super(200, 'DataParseError', msg)
    }
}

export class HttpError extends CoreError<string>{
    constructor(msg: string) {
        super(200, msg)
    }
}